package com.ruoyi.nsh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.nsh.service.SqlQueryService;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sqlquery")
public class SqlQueryController extends BaseController {
    
    @Autowired
    private SqlQueryService sqlQueryService;
    
    @PostMapping("/execute")
    public TableDataInfo executeSql(@RequestParam String sql, 
                                  @RequestParam String dataSource,
                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "20") Integer pageSize) {
        List<Map<String, Object>> list = sqlQueryService.executeQuery(sql, dataSource);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setRows(list);
        rspData.setTotal(sqlQueryService.queryTotal(sql, dataSource));
        return rspData;
    }

    @GetMapping("/tables")
    public AjaxResult getTableList(@RequestParam String dataSource) {
        return AjaxResult.success(sqlQueryService.getTableList(dataSource));
    }

    @GetMapping("/schema/list")
    public AjaxResult getSchemaList(@RequestParam String dataSource) {
        List<String> schemas = sqlQueryService.getSchemaList(dataSource);
        return AjaxResult.success(schemas);
    }
} 
